/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web;

import hu.glassy.web.auth.ApplicationSession;
import hu.glassy.web.auth.ApplicationSessionStoreProvider;
import hu.glassy.web.page.account.details.AccountDetailsPage;
import hu.glassy.web.page.account.edit.AccountEditPage;
import hu.glassy.web.page.account.logout.AccountLogoutPage;
import hu.glassy.web.page.account.registration.AccountActivationPage;
import hu.glassy.web.page.account.registration.AccountRegistrationPage;
import hu.glassy.web.page.account.registration.AccountResetPasswordPage;
import hu.glassy.web.page.error.PageNotFoundPage;
import hu.glassy.web.page.home.PublicHomePage;
import hu.glassy.web.page.home.UserHomePage;
import hu.glassy.web.page.movie.edit.MovieAddPage;
import hu.glassy.web.page.movie.edit.MovieEditPage;
import hu.glassy.web.page.movie.list.MovieListPage;
import hu.glassy.web.page.person.edit.PersonAddPage;
import hu.glassy.web.page.person.edit.PersonEditPage;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.settings.IRequestCycleSettings;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.util.file.Folder;
import org.wicketstuff.htmlcompressor.HtmlCompressingMarkupFactory;

/**
 * Application object for your web application. If you want to run this
 * application without deploying, run the Start class.
 *
 * @see wicket.myproject.Start#main(String[])
 */
public class GlassyApplication extends AuthenticatedWebApplication
{

  private Folder imageUploadFolder = null;

  /**
   * Constructor
   */
  public GlassyApplication()
  {
  }

  /**
   * @see wicket.Application#getHomePage()
   */
  @SuppressWarnings(
          {
            "unchecked", "rawtypes"
          })
  @Override
  public Class getHomePage()
  {
    return PublicHomePage.class;
  }

  @Override
  protected void init()
  {
    super.init();

    getComponentInstantiationListeners().add(new SpringComponentInjector(this));

    setSessionStoreProvider(new ApplicationSessionStoreProvider());

    // getApplicationSettings().setPageExpiredErrorPage(PageExpiredPage.class);
    // getApplicationSettings().setAccessDeniedPage(AccessDeniedPage.class);
    // getApplicationSettings().setInternalErrorPage(InternalErrorPage.class);
    // getSecuritySettings().setUnauthorizedComponentInstantiationListener(this);
    // getSecuritySettings().setAuthorizationStrategy(new IDMAuthorizationStrategy(this, menu.values()));
    getResourceSettings().setThrowExceptionOnMissingResource(true);
    getPageSettings().setVersionPagesByDefault(false);

    getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
    // The following row will result that HTML comments will not be rendered in the output
    getMarkupSettings().setCompressWhitespace(true);
    getMarkupSettings().setStripComments(true);
    getMarkupSettings().setStripWicketTags(true);
    // Using HTML compression...
    getMarkupSettings().setMarkupFactory(new HtmlCompressingMarkupFactory());

    // Make the pages stateless...
    getRequestCycleSettings().setRenderStrategy(IRequestCycleSettings.RenderStrategy.ONE_PASS_RENDER);

    // The following row sets the internal error page
    // getExceptionSettings().setUnexpectedExceptionDisplay(IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE);
    // The place of uploaded images
    imageUploadFolder = new Folder("images");
    // Ensure folder exists
    imageUploadFolder.mkdirs();

    mountPages();
  }

  @Override
  protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass()
  {
    return ApplicationSession.class;
  }

  @Override
  protected Class<? extends WebPage> getSignInPageClass()
  {
    return PublicHomePage.class;
  }

  /**
   * Set up the page urls here...
   */
  private void mountPages()
  {
    mountPage("/404", PageNotFoundPage.class);

    //mountPage("/", PublicHomePage.class);
    mountPage("/home", PublicHomePage.class);

    mountPage("/user/home", UserHomePage.class);

    mountPage("/account/registration", AccountRegistrationPage.class);
    mountPage("/account/activation", AccountActivationPage.class);
    mountPage("/account/logout", AccountLogoutPage.class);
    mountPage("/account/resetpassword", AccountResetPasswordPage.class);
    mountPage("/account/details", AccountDetailsPage.class);
    mountPage("/account/edit", AccountEditPage.class);

    mountPage("/person/add", PersonAddPage.class);
    mountPage("/person/edit/${id}", PersonEditPage.class);

    mountPage("/movie/list", MovieListPage.class);
    mountPage("/movie/add", MovieAddPage.class);
    mountPage("/movie/edit/${id}", MovieEditPage.class);
  }

  /**
   * @return the folder for image uploads
   */
  public Folder getImageUploadFolder()
  {
    return imageUploadFolder;
  }

  public static GlassyApplication get()
  {
    return (GlassyApplication) AuthenticatedWebApplication.get();
  }
}
